<?php

!defined('IN_VIFRAME') && die('Access Denied.');

Vi::import('Exception : System.File.File');

class File extends TLib {

	const
		ERR_SILENT        = '.Silent',
		ERR_HALT          = '.Halt';
	
	public static function get($key, $config = array()) {
		$config = self::_filterConfigParam($config, array('FilterAlias' => VI_FILTERALIAS));
		$path   = Vi::getImportFilesByPath($key, $config['FilterAlias']);
		
		return self::getByPath($path[0], $config);
	}
	
	public static function getByPath($path, $config = array()) {
		$config = self::_filterConfigParam($config, array(
			'OnError' => VI_FILE_ONERROR,
			'Error'   => 'Get'
		));
		
		$return = @file_get_contents($path);
		if ($return) {
			return $return;
		} else {
			if ($config['OnError'] = self::ERR_HALT) {
				throw new EFile($config['Error']);
			}
		}
	}
	
	public static function getInclude($key, $config = array()) {
		$config = self::_filterConfigParam($config, array('FilterAlias' => VI_FILTERALIAS));
		$path   = Vi::getImportFilesByPath($key, $config['FilterAlias']);
		
		return self::getIncludeByPath($path[0], $config);
	}
	
	public static function getIncludeByPath($path, $config = array()) {
		$config = self::_filterConfigParam($config, array(
			'OnError' => VI_FILE_ONERROR,
			'Error'   => 'Include' 
		));
		
		$return = @include($path[0]);
		if ($return) {
			return $return;
		} else {
			if ($config['OnError'] = self::ERR_HALT) {
				throw new EFile($config['Error']);
			}
		}
	}
	
	public static function getModifiedTime($key, $config = array()){
		$config = self::_filterConfigParam($config, array('FilterAlias' => VI_FILTERALIAS));
		$path   = Vi::getImportFilesByPath($key, $config['FilterAlias']);
		
		return self::getModifiedTimeByPath($path[0], $config);
	}
	
	public static function getModifiedTimeByPath($path, $config = array()) {
		$config = self::_filterConfigParam($config, array(
			'OnError' => VI_FILE_ONERROR,
			'Error'   => 'ModifiedTime' 
		));
		
		$return = @filemtime($path);
		if ($return) {
			return $return;
		} else {
			if ($config['OnError'] = self::ERR_HALT) {
				throw new EFile($config['Error']);
			}
		}
	}
	
	public static function setFile($key, $value, $config = array()) {
		$config = self::_filterConfigParam($config, array('FilterAlias' => VI_FILTERALIAS));
		$path   = Vi::getImportFilesByPath($key, $config['FilterAlias']);
		
		return self::setFileByPath($path, $value, $config);
	}
	
	public static function setFileByPath($path, $value, $config = array()) {
		if (is_array($value))
			$value = serialize($value);
			
		$config = self::_filterConfigParam($config, array(
			'OnError' => VI_FILE_ONERROR,
			'Error'   => 'Set' 
		));
		
		$return = @file_put_contents($path, $value);
		if ($return) {
			return $return;
		} else {
			if ($config['OnError'] = self::ERR_HALT) {
				throw new EFile($config['Error']);
			}
		}
	}
	
	public static function removeFile($key, $config = array()) {
		$config = self::_filterConfigParam($config, array('FilterAlias' => VI_FILTERALIAS));
		$path   = Vi::getImportFilesByPath($key, $config['FilterAlias']);
		
		return self::removeFileByPath($path[0], $config);
	}
	
	public static function removeFileByPath($path, $config = array()) {
		$config = self::_filterConfigParam($config, array(
			'OnError' => VI_FILE_ONERROR,
			'Error'   => 'Remove' 
		));
		
		$return = @unlink($path);
		if ($return) {
			return $return;
		} else {
			if ($config['OnError'] = self::ERR_HALT) {
				throw new EFile($config['Error']);
			}
		}
	}

	public static function createDir($path, $createIndex = true, $config = array()) {	
		$realPath              = FN_ROOT.$path;
		$config['CreateIndex'] = $createIndex;
		 
		return self::_createDirByRealPath($realPath, $createIndex, $mode);  
	}
	
	protected static function _createDirByRealPath($realPath, $config) {
		$config = self::_filterConfigParam($config, array(
			'IndexMode' => 0777,
			'OnError'   => VI_FILE_ONERROR,
			'Error'     => 'CreateDir' 
		));
		
		if (!is_dir($path)) {
			if (self::_createDirByRealPath(dirname($realPath), $config)) {
				if (false === @mkdir($realPath, $config['Mode'])) {
					if ($config['OnError'] = self::ERR_HALT) {
						throw new EFile($config['Error']);
					}
				}
				if($config['CreateIndex']) {
					$indexPath = $realPath.DIRECTORY_SEPARATOR.'index.html';
					@touch($indexPath);
					@chmod($indexPath, $config['Mode']);
				}
			} else {
				if ($config['OnError'] = self::ERR_HALT) {
					throw new EFile($config['Error']);
				}
			}
		} else {
			return true;
		}
	}
}